package com.atguigu.gmall.common.config.pool;

import com.atguigu.gmall.common.config.pool.properties.AppThreadPoolProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.concurrent.*;

@EnableConfigurationProperties(AppThreadPoolProperties.class)
//1、开启 AppThreadPoolProperties 的属性配置绑定
//2、把 AppThreadPoolProperties 放到容器中
@Configuration
public class AppThreadPoolConfiguration {

    @Primary
    @Bean
    public ThreadPoolExecutor
        corePool(AppThreadPoolProperties threadPoolProperties,
                           @Value("${spring.application.name}") String applicationName){
        //1、线程池想要对接监控平台。
        //2、监控平台自己从redis中拉取各种指标，进行可视化展示
        //3、
        return new ThreadPoolExecutor(threadPoolProperties.getCorePoolSize(),
                threadPoolProperties.getMaximumPoolSize(),
                threadPoolProperties.getKeepAliveTime(),
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(threadPoolProperties.getWorkQueueSize()),
                new ThreadFactory() { //线程池中所有的线程都是它new的
                    int i = 0; //单线程
                    @Override
                    public Thread newThread(Runnable r) {
                        // 分布式全线程数量限制：redis.incr(thread-count) < 100000

                        //利用层层封装完整所有功能
//                        Runnable runnable = new Runnable() {
//                            @Override
//                            public void run() { //根据cpu调度
//
//                            }
//                        };
                        Thread thread = new Thread(()->{
                            //redis.incr("running...") 任务前置逻辑增强
                            r.run(); //跑真实任务
                            //redis.decr("running...") 任务后置逻辑增强
                        });
                        thread.setName("【"+applicationName+"】-apppool-"+ (i++));
                        return thread;
                    }
                },
                new ThreadPoolExecutor.CallerRunsPolicy());
    }



//    @Bean
//    public ThreadPoolExecutor
//      otherPool(AppThreadPoolProperties threadPoolProperties,
//             @Value("${spring.application.name}") String applicationName){
//        //1、线程池想要对接监控平台。
//        //2、监控平台自己从redis中拉取各种指标，进行可视化展示
//        //3、
//        return new ThreadPoolExecutor(threadPoolProperties.getCorePoolSize(),
//                threadPoolProperties.getMaximumPoolSize(),
//                threadPoolProperties.getKeepAliveTime(),
//                TimeUnit.MILLISECONDS,
//                new LinkedBlockingQueue<>(threadPoolProperties.getWorkQueueSize()),
//                new ThreadFactory() { //线程池中所有的线程都是它new的
//                    int i = 0; //单线程
//                    @Override
//                    public Thread newThread(Runnable r) {
//                        // 分布式全线程数量限制：redis.incr(thread-count) < 100000
//
//                        //利用层层封装完整所有功能
////                        Runnable runnable = new Runnable() {
////                            @Override
////                            public void run() { //根据cpu调度
////
////                            }
////                        };
//                        Thread thread = new Thread(()->{
//                            //redis.incr("running...") 任务前置逻辑增强
//                            r.run(); //跑真实任务
//                            //redis.decr("running...") 任务后置逻辑增强
//                        });
//                        thread.setName("【"+applicationName+"】-apppool-"+ (i++));
//                        return thread;
//                    }
//                },
//                new ThreadPoolExecutor.CallerRunsPolicy());
//    }

}
